package com.ogilvy.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ogilvy.cms.bean.CmsComponent;
import com.ogilvy.cms.bean.CmsLog;
import com.ogilvy.cms.bean.CmsUser;
import com.ogilvy.cms.util.ServiceDispatcher;
import com.ogilvy.common.JSONBean;
import com.ogilvy.common.MetaData;
import com.ogilvy.common.UtilTools;
import com.ogilvy.frame.action.BaseAction;
import com.ogilvy.frame.util.CmsConstants;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("cms.action.CmsComponentAction")
public class CmsComponentAction extends AdminAction{
	@Autowired
	protected ServiceDispatcher serviceDispatcher;
	//protected componentService componentService;
	private String rows;//前台以JSON的形式传递数据
	private int start;//分页开始
	private int limit;//每页显示量
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	//实现点击左边树右边显示
	public String index(){
		return "index";
	}
	private CmsComponent Component=new CmsComponent();
	List<CmsComponent> Complist;
	/**到列表页面*/
	public void query(){
		if(rows==null){
			Complist=serviceDispatcher.getCmsComponentService().query(Component);
		}
		else{
			net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
			Map<String,String> screenMap=new HashMap<String, String>();
			screenMap.put("cmcComponentNo", js.getString("cmcComponentNo"));
			screenMap.put("cmcComponentName", js.getString("cmcComponentName"));
			Complist=serviceDispatcher.getCmsComponentService().queryByScreen(screenMap);
		}
		
		//采用集合分页
		List pageList = UtilTools.queryForPage(start, limit, Complist);
		JSONBean jbean = new JSONBean();
		MetaData md = new MetaData();
		
		md.setIdProperty("cmcId");//关联将要显示列表对应的bean实体的主键ID
		md.reflectFields("com.ogilvy.cms.bean.CmsComponent");//关联将要显示列表对应的bean实体
		jbean.setMetaData(md);
		jbean.setRows(pageList);
		jbean.setSuccess(true);
		jbean.setTotal(Complist.size());

		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");

		//String jsonString = UtilTools.encodeObject2Json(jbean);
		String jsonString = UtilTools.encodeObject2Json(jbean,"MM/dd/yyyy HH:mm:ss");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**添加*/
	public void add(){
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		
		String cmcComponentNo=js.getString("cmcComponentNo");
		String cmcComponentName=js.getString("cmcComponentName");
		String cmcComponentType=js.getString("cmcComponentType");
		String cmcComponentContent=js.getString("cmcComponentContent");
		String isedi=js.getString("cmcIsEdit");
		int cmcIsEdit=Integer.parseInt(isedi);
		String pubsta=js.getString("cmcPubStatus");
		int cmcPubStatus=Integer.parseInt(pubsta);
		
		CmsUser cmsuser = getCmsUser();
		String cmcLastuser =cmsuser.getCmuName();//获取session中对象的姓名
	
		Date getDate=new Date();
		Date cmcLasttime=getDate;
		
		Component.setCmcComponentNo(cmcComponentNo);
		Component.setCmcComponentName(cmcComponentName);
		Component.setCmcComponentType(cmcComponentType);
		Component.setCmcComponentContent(cmcComponentContent);
		Component.setCmcIsEdit(cmcIsEdit);
		Component.setCmcPubStatus(cmcPubStatus);
		Component.setCmcLastuser(cmcLastuser);
		Component.setCmcLasttime(cmcLasttime);
		
		boolean isHave=serviceDispatcher.getCmsComponentService().checkComponentNo(cmcComponentNo);
		if(isHave==false){
			CmsComponent comp=serviceDispatcher.getCmsComponentService().add(Component);
			JSONBean bean = new JSONBean();
			bean.setSuccess(true);
			// 需要新建一个具有id的vo返回给前台以便通知前台保存成功，不然前台一直处于编辑状态
			List rows = new ArrayList();
			rows.add(comp);
			bean.setRows(rows);
			//String jsonString = UtilTools.encodeObject2Json(bean);
			String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
			/**
			 * 系统日志
			 */
			CmsLog  log = UtilTools.buildLog("模板组件", CmsLog.Add, "添加组件", cmcLastuser);
	        serviceDispatcher.getCmsLogService().add(log);
			PrintWriter out;
			try {
				out = ServletActionContext.getResponse().getWriter();
				out.print(jsonString);
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			StringBuffer jsonString =new StringBuffer();
            jsonString.append("{");
            jsonString.append("success:true,");
            jsonString.append("isHave:true,");
            jsonString.append("msg:'对不起,该组件标识已存在!'");
            jsonString.append("}");
        	 PrintWriter out;
 	        try {
 	            out = ServletActionContext.getResponse().getWriter();
 	            out.print(jsonString);
 	            out.flush();
 	            out.close();
 	        } catch (IOException e) {
 	            e.printStackTrace();
 	        }
		}
		
		
	}
	/**删除*/
	public void del(){
		net.sf.json.JSONArray objArr = net.sf.json.JSONArray.fromObject(rows);
        for(int i=0;i<objArr.size();i++)
        {
        	net.sf.json.JSONObject js=objArr.getJSONObject(i);
        	serviceDispatcher.getCmsComponentService().del(js.getInt("cmcId"));
        }
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		CmsUser cmsuser = getCmsUser();
		String cmcLastuser =cmsuser.getCmuName();//获取session中对象的姓名
		/**
		 * 系统日志
		 */
		CmsLog  log = UtilTools.buildLog("模板组件", CmsLog.DELETE, "删除组件", cmcLastuser);
        serviceDispatcher.getCmsLogService().add(log);
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**修改*/
	public void modify(){
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		String Id=js.getString("cmcId");
		int cmcId=Integer.parseInt(Id);
		String cmcComponentNo=js.getString("cmcComponentNo");
		String copycmcComponentNo=js.getString("copycmcComponentNo");
		String cmcComponentName=js.getString("cmcComponentName");
		String cmcComponentType=js.getString("cmcComponentType");
		String cmcComponentContent=js.getString("cmcComponentContent");
		String isedi=js.getString("cmcIsEdit");
		int cmcIsEdit=Integer.parseInt(isedi);
		String pubsta=js.getString("cmcPubStatus");
		int cmcPubStatus=Integer.parseInt(pubsta);
		CmsUser cmsuser = getCmsUser();
		String cmcLastuser =cmsuser.getCmuName();//获取session中对象的姓名
		
		Date getDate=new Date();
		Date cmcLasttime=getDate;
		
		Component.setCmcId(cmcId);
		Component.setCmcComponentNo(cmcComponentNo);
		Component.setCmcComponentName(cmcComponentName);
		Component.setCmcComponentType(cmcComponentType);
		Component.setCmcComponentContent(cmcComponentContent);
		Component.setCmcIsEdit(cmcIsEdit);
		Component.setCmcPubStatus(cmcPubStatus);
		Component.setCmcLastuser(cmcLastuser);
		Component.setCmcLasttime(cmcLasttime);
		
		if(copycmcComponentNo.equals(cmcComponentNo)){
			serviceDispatcher.getCmsComponentService().modify(Component);
			JSONBean bean = new JSONBean();
			bean.setSuccess(true);
			//String jsonString = UtilTools.encodeObject2Json(bean);
			String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
			/**
			 * 系统日志
			 */
			CmsLog  log = UtilTools.buildLog("模板组件", CmsLog.MODIFY, "修改组件", cmcLastuser);
	        serviceDispatcher.getCmsLogService().add(log);
			PrintWriter out;
			try {
				out = ServletActionContext.getResponse().getWriter();
				out.print(jsonString);
				out.flush();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			boolean isHave=serviceDispatcher.getCmsComponentService().checkComponentNo(cmcComponentNo);
			if(isHave==false){
				serviceDispatcher.getCmsComponentService().modify(Component);
				JSONBean bean = new JSONBean();
				bean.setSuccess(true);
				//String jsonString = UtilTools.encodeObject2Json(bean);
				String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
				/**
				 * 系统日志
				 */
				CmsLog  log = UtilTools.buildLog("模板组件", CmsLog.MODIFY, "修改组件", cmcLastuser);
		        serviceDispatcher.getCmsLogService().add(log);
				PrintWriter out;
				try {
					out = ServletActionContext.getResponse().getWriter();
					out.print(jsonString);
					out.flush();
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				StringBuffer jsonString =new StringBuffer();
	            jsonString.append("{");
	            jsonString.append("success:true,");
	            jsonString.append("isHave:true,");
	            jsonString.append("msg:'对不起,该组件标识已存在!'");
	            jsonString.append("}");
	        	 PrintWriter out;
	 	        try {
	 	            out = ServletActionContext.getResponse().getWriter();
	 	            out.print(jsonString);
	 	            out.flush();
	 	            out.close();
	 	        } catch (IOException e) {
	 	            e.printStackTrace();
	 	        }
			}
		}
		
		
	}
}
